#include "stdafx.h"
#include <string.h>
#include "GraphicFunctions.h"
#include "IniReader.h"

#include "Theme.h"

#ifdef _DEBUG
#include "crtdbg.h"
#endif

// skin
HBITMAP Theme::hbmSkin = NULL;
HDC     Theme::hdcSkin = NULL;
HBITMAP Theme::hbmTemp = NULL;
HDC     Theme::hdcTemp = NULL;
int     Theme::iSkinWidth = 0;
int     Theme::iSkinHeight = 0;
COLORREF Theme::rgbTransparentColor = 0;

// pictures
int      Theme::ListPictureWidth;
int      Theme::ListPictureHeight;

int      Theme::SpecialIconsWidth;
int      Theme::SpecialIconsHeight;
int      Theme::SpecialIconsY;

// fonts
HFONT Theme::PrimaryListFont = NULL;
HFONT Theme::DialingNoListFont = NULL;
HFONT Theme::SecondaryListFont = NULL;
HFONT Theme::DetailsHeaderFont = NULL;
HFONT Theme::DetailsHeaderFont2 = NULL;
HFONT Theme::DetailsItemFont = NULL;
HFONT Theme::DetailsFooterFont = NULL;
HFONT Theme::GroupFont = NULL;
HFONT Theme::ListIndicatorFont = NULL;
HFONT Theme::KeyboardFont = NULL;
HFONT Theme::T9ButtonsFont = NULL;
HFONT Theme::T9TextFont = NULL;
HFONT Theme::CategoriesFont = NULL;
HFONT Theme::CategoriesFontBold = NULL;
int Theme::PrimaryListFontSize;
int Theme::DialingNoListFontSize;
int Theme::SecondaryListFontSize;
int Theme::DetailsHeaderFontSize;
int Theme::DetailsHeaderFont2Size;
int Theme::DetailsItemFontSize;
int Theme::DetailsFooterFontSize;
int Theme::GroupFontSize;
int Theme::ListIndicatorFontSize;
int Theme::KeyboardFontSize;
int Theme::T9ButtonsFontSize;
int Theme::T9TextFontSize;
int Theme::CategoriesFontSize;

int Theme::iListScrollbarWidth = 30;
int Theme::iListScrollbarIndicatorWidth = 3;
int Theme::iListItemSize = 40;
int Theme::iListItemIndent = 8;
int Theme::iListSecondaryWidth = 40;
int Theme::iListGroupSize = 17;
int Theme::iListGroupItemIndent = 14;
int Theme::iLongTapIndWidth = 6;
bool Theme::bLongTapAtRight = true;

int Theme::iDetailsHeaderSize = 60;
int Theme::iDetailsHeaderSpacing = 3;
int Theme::iDetailsPictureWidth = 40;
int Theme::iDetailsItemSize = 40;
int Theme::iDetailsIndent = 5;
int Theme::iDetailsTypeWidth = 40;
int Theme::iDetailsSubActionWidth = 40;
int Theme::iDetailsBackAreaSize = 40;
int Theme::iDetailsFooterSpacing = 3;
int Theme::iMinimizeAreaSize = 32;
int Theme::iCategoriesItemHeight = 30;
int Theme::iSearchKeyboardHeight = 160;

// Theme::titlebar
COLORREF Theme::rgbTitlebarBackground;
COLORREF Theme::rgbTitlebarText;
COLORREF Theme::rgbTitlebarSignal;
COLORREF Theme::rgbTitlebarBattery;
HBRUSH Theme::hbrTitlebarBackground = NULL;
HBRUSH Theme::hbrTitlebarText = NULL;
HBRUSH Theme::hbrTitlebarSignal = NULL;
HBRUSH Theme::hbrTitlebarBattery = NULL;
HFONT Theme::TitlebarFont = NULL;
int   Theme::TitlebarFontSize;
int   Theme::TitlebarHeight = 16;
// titlebar skin elements
int   Theme::TitlebarSkinWIDTH;
int   Theme::TitlebarSkinICON_SPACING;
int   Theme::TitlebarSkinBATTERY_X_OFFSET;
int   Theme::TitlebarSkinBATTERY_WIDTH;
int   Theme::TitlebarSkinSPEAKER_OFF_X_OFFSET;
int   Theme::TitlebarSkinSPEAKER_OFF_WIDTH;
int   Theme::TitlebarSkinSPEAKER_ON_X_OFFSET;
int   Theme::TitlebarSkinSPEAKER_ON_WIDTH;
int   Theme::TitlebarSkinVIBRATE_X_OFFSET;
int   Theme::TitlebarSkinVIBRATE_WIDTH;
int   Theme::TitlebarSkinBLUETOOTH_X_OFFSET;
int   Theme::TitlebarSkinBLUETOOTH_WIDTH;
int   Theme::TitlebarSkinWIFI_X_OFFSET;
int   Theme::TitlebarSkinWIFI_WIDTH;
int   Theme::TitlebarSkinLOCK_X_OFFSET;
int   Theme::TitlebarSkinLOCK_WIDTH;
int   Theme::TitlebarSkinALARM_X_OFFSET;
int   Theme::TitlebarSkinALARM_WIDTH;
int   Theme::TitlebarSkinA2DP_X_OFFSET;
int   Theme::TitlebarSkinA2DP_WIDTH;
int   Theme::TitlebarSkinCONNECTION_X_OFFSET;
int   Theme::TitlebarSkinCONNECTION_WIDTH;
int   Theme::TitlebarSkinBATTERY_BAR_OFFSETX;
int   Theme::TitlebarSkinBATTERY_BAR_OFFSETY;
int   Theme::TitlebarSkinBATTERY_BAR_WIDTH;
int   Theme::TitlebarSkinBATTERY_BAR_HEIGHT;

// Theme::list
HBRUSH Theme::hbrListBackground = NULL;
HBRUSH Theme::hbrListGroupBackground = NULL;
COLORREF Theme::rgbListGroupText;
COLORREF Theme::rgbListItemBackground;
HBRUSH Theme::hbrListItemBackground = NULL;
COLORREF Theme::rgbListItemHoverBackground1;
COLORREF Theme::rgbListItemHoverBackground2;
COLORREF Theme::rgbListItemText;
COLORREF Theme::rgbListItemFavoriteText;
COLORREF Theme::rgbListItemSecondaryText;
COLORREF Theme::rgbListItemSecondaryText2;
COLORREF Theme::rgbListItemNumberText;
HBRUSH Theme::hbrListItemForeground = NULL;
HPEN Theme::hpenListItemForeground = NULL;
COLORREF Theme::rgbListItemSelectedBackground1;
COLORREF Theme::rgbListItemSelectedBackground2;
COLORREF Theme::rgbListItemSelectedText;
COLORREF Theme::rgbListItemSelectedShadow;
HBRUSH Theme::hbrListItemSeparator = NULL;
COLORREF Theme::rgbListIndicatorText;
COLORREF Theme::rgbListScrollbarIndicator;
HBRUSH   Theme::hbrListScrollbarIndicator;
COLORREF Theme::rgbLongTap;
HBRUSH   Theme::hbrLongTap;
COLORREF Theme::rgbLongTapFull;
HBRUSH   Theme::hbrLongTapFull;

COLORREF Theme::rgbCallHistoryMissed;
COLORREF Theme::rgbCallHistoryIncoming;
COLORREF Theme::rgbCallHistoryOutgoing;

COLORREF Theme::rgbDetailsBackground1;
COLORREF Theme::rgbDetailsBackground2;
HBRUSH   Theme::hbrDetailsBackground;
COLORREF Theme::rgbDetailsHoverBackground1;
COLORREF Theme::rgbDetailsHoverBackground2;
COLORREF Theme::rgbDetailsHeaderShadow;
COLORREF Theme::rgbDetailsHeaderText;
COLORREF Theme::rgbDetailsItemShadow;
COLORREF Theme::rgbDetailsItemText;
COLORREF Theme::rgbDetailsDefaultNumber;
HBRUSH Theme::hbrDetailItemSeparator = NULL;

// Theme::popup
COLORREF Theme::rgbPopupText;
COLORREF Theme::rgbPopupBackground;
COLORREF Theme::rgbPopupGrid;
HBRUSH Theme::hbrPopupBackground = NULL;
HBRUSH Theme::hbrPopupForeground = NULL;
HPEN Theme::hpenPopupGrid = NULL;

// Theme::menu Theme::bar
bool Theme::MenuStretch;
int Theme::iMenuButtonHeight;
int Theme::iMenuButtonWidth;
int Theme::iMenuButtonsPerRow;
int Theme::iMenuSkinY;

// detail icons
int Theme::iDetailIconsY;
int Theme::iDetailIconsW;
int Theme::iDetailIconsH;
int Theme::iDetailIconsPerPlane;
bool Theme::HasSmallIcons; // lcm
int Theme::iDetailSmallIconsY; // lcm
int Theme::iDetailSmallIconsW; // lcm
int Theme::iDetailSmallIconsH; // lcm

void Theme::Create(HWND hWnd)
{
  if (hdcSkin != NULL)
    throw TEXT("Theme::Create() hdcSkin is not NULL!");
  if (hbmSkin != NULL)
    throw TEXT("Theme::Create() hbmSkin is not NULL!");

  // Skin
  TCHAR szSkinFileName[MAX_PATH];
  ::GetModuleFileName(NULL, szSkinFileName, MAX_PATH);
  TCHAR * pstr = _tcsrchr(szSkinFileName, TEXT('\\'));
  if (pstr) *(++pstr) = 0;
  ::StringCchCat(szSkinFileName, MAX_PATH, INI_THEME_PATH);
  ::StringCchCat(szSkinFileName, MAX_PATH, IniReader::getString(IniReader::sectionMain, TEXT("Skin"), TEXT("default")));
  ::StringCchCat(szSkinFileName, MAX_PATH, TEXT(".png"));

  // Load Skin
  hbmSkin = ::SHLoadImageFile(szSkinFileName);
  if (hbmSkin == NULL)
    throw TEXT("Unable to load skin file!");

  BITMAP bmpInfo;
  GetObject(hbmSkin, sizeof(bmpInfo), (LPVOID)&bmpInfo);
  iSkinWidth = (int)bmpInfo.bmWidth;
  iSkinHeight = (int)bmpInfo.bmHeight;

  // Create skin DC
  HDC hdc = ::GetDC(hWnd);

  hdcSkin = ::CreateCompatibleDC(hdc);
  SelectObject(hdcSkin, hbmSkin);

  hdcTemp = ::CreateCompatibleDC(hdc);

  // Release Window DC
  ::ReleaseDC(hWnd, hdc);

  rgbTransparentColor = IniReader::getColor(IniReader::sectionSkin, TEXT("TransparentColor"), TEXT("000000"));

  // Create pictures
  ListPictureWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("ListPictureWidth"), TEXT("30"));
  ListPictureHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("ListPictureHeight"), TEXT("40"));

  SpecialIconsWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("SpecialIconsWidth"), TEXT("32"));
  SpecialIconsHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("SpecialIconsHeight"), TEXT("32"));
  SpecialIconsY = IniReader::getInt(IniReader::sectionSkin, TEXT("SpecialIconsY"), TEXT("256"));

  // Colors, Brushes & Fonts
  // Create fonts
  const TCHAR* fontName;

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("PrimaryListFont"), TEXT("Tahoma"));
  PrimaryListFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("PrimaryListFontSize"), TEXT("20"));
  PrimaryListFont = GraphicFunctions::BuildFont(fontName, PrimaryListFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("DialingNoListFont"), TEXT("Tahoma"));
  DialingNoListFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DialingNoListFontSize"), TEXT("11"));
  DialingNoListFont = GraphicFunctions::BuildFont(fontName, DialingNoListFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("SecondaryListFont"), TEXT("Tahoma"));
  SecondaryListFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("SecondaryListFontSize"), TEXT("12"));
  SecondaryListFont = GraphicFunctions::BuildFont(fontName, SecondaryListFontSize, TRUE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("DetailsHeaderFont"), TEXT("Tahoma"));
  DetailsHeaderFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsHeaderFontSize"), TEXT("20"));
  DetailsHeaderFont = GraphicFunctions::BuildFont(fontName, DetailsHeaderFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("DetailsHeaderFont2"), TEXT("Tahoma"));
  DetailsHeaderFont2Size = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsHeaderFont2Size"), TEXT("13"));
  DetailsHeaderFont2 = GraphicFunctions::BuildFont(fontName, DetailsHeaderFont2Size, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("DetailsItemFont"), TEXT("Tahoma"));
  DetailsItemFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsItemFontSize"), TEXT("20"));
  DetailsItemFont = GraphicFunctions::BuildFont(fontName, DetailsItemFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("DetailsFooterFont"), TEXT("Tahoma"));
  DetailsFooterFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsFooterFontSize"), TEXT("13"));
  DetailsFooterFont = GraphicFunctions::BuildFont(fontName, DetailsFooterFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("GroupFont"), TEXT("Tahoma"));
  GroupFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("GroupFontSize"), TEXT("13"));
  GroupFont = GraphicFunctions::BuildFont(fontName, GroupFontSize, TRUE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("ListIndicatorFont"), TEXT("Tahoma"));
  ListIndicatorFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("ListIndicatorFontSize"), TEXT("55"));
  ListIndicatorFont = GraphicFunctions::BuildFont(fontName, ListIndicatorFontSize, TRUE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("KeyboardFont"), TEXT("Tahoma"));
  KeyboardFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("KeyboardFontSize"), TEXT("40"));
  KeyboardFont = GraphicFunctions::BuildFont(fontName, KeyboardFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("T9ButtonsFont"), TEXT("Tahoma"));
  T9ButtonsFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("T9ButtonsFontSize"), TEXT("20"));
  T9ButtonsFont = GraphicFunctions::BuildFont(fontName, T9ButtonsFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("T9TextFont"), TEXT("Tahoma"));
  T9TextFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("T9TextFontSize"), TEXT("13"));
  T9TextFont = GraphicFunctions::BuildFont(fontName, T9TextFontSize, FALSE, FALSE);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("CategoriesFont"), TEXT("Tahoma"));
  CategoriesFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("CategoriesFontSize"), TEXT("20"));
  CategoriesFont = GraphicFunctions::BuildFont(fontName, CategoriesFontSize, FALSE, FALSE);
  CategoriesFontBold = GraphicFunctions::BuildFont(fontName, (CategoriesFontSize * 5) / 4, TRUE, FALSE);

  iListScrollbarWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("ListScrollbarWidth"), TEXT("30"));
  iListScrollbarIndicatorWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("ListScrollbarIndicatorWidth"), TEXT("2"));
  iListItemSize = IniReader::getInt(IniReader::sectionSkin, TEXT("ListItemSize"), TEXT("40"));
  iListItemIndent = IniReader::getInt(IniReader::sectionSkin, TEXT("ListItemIndent"), TEXT("4"));
  iListSecondaryWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("ListSecondaryWidth"), TEXT("40"));
  iListGroupSize = IniReader::getInt(IniReader::sectionSkin, TEXT("ListGroupSize"), TEXT("17"));
  iListGroupItemIndent = IniReader::getInt(IniReader::sectionSkin, TEXT("ListGroupItemIndent"), TEXT("14"));
  iLongTapIndWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("LongTapIndWidth"), TEXT("6"));
  bLongTapAtRight = IniReader::getBool(IniReader::sectionSkin, TEXT("LongTapAtRight"), TEXT("1"));

  iDetailsHeaderSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsHeaderSize"), TEXT("70"));
  iDetailsPictureWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsPictureWidth"), TEXT("50"));
  iDetailsItemSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsItemSize"), TEXT("40"));
  iDetailsIndent = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsIndent"), TEXT("5"));
  iDetailsTypeWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsTypeWidth"), TEXT("40"));
  iDetailsSubActionWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsSubActionWidth"), TEXT("40"));
  iDetailsBackAreaSize = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsBackAreaSize"), TEXT("40"));
  iDetailsFooterSpacing = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailsFooterSpacing"), TEXT("3"));

  iMinimizeAreaSize = IniReader::getInt(IniReader::sectionSkin, TEXT("MinimizeAreaSize"), TEXT("32"));
  iCategoriesItemHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("CategoriesItemHeight"), TEXT("40"));
  iSearchKeyboardHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("T9KeyboardHeight"), TEXT("160"));

  if (ListPictureHeight > iListItemSize)
    ListPictureHeight = iListItemSize;

  rgbTitlebarBackground = IniReader::getColor(
    IniReader::sectionSkin, TEXT("TitlebarBackground"), TEXT("000000"));
  hbrTitlebarBackground = CreateSolidBrush(rgbTitlebarBackground);
  rgbTitlebarText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("TitlebarText"), TEXT("ffffff"));
  hbrTitlebarText = CreateSolidBrush(rgbTitlebarText);
  rgbTitlebarSignal = IniReader::getColor(
    IniReader::sectionSkin, TEXT("TitlebarSignal"), TEXT("ffffff"));
  hbrTitlebarSignal = CreateSolidBrush(rgbTitlebarSignal);
  rgbTitlebarBattery = IniReader::getColor(
    IniReader::sectionSkin, TEXT("TitlebarBattery"), TEXT("ffffff"));
  hbrTitlebarBattery = CreateSolidBrush(rgbTitlebarBattery);

  fontName = IniReader::getString(IniReader::sectionSkin, TEXT("TitlebarFont"), TEXT("Tahoma"));
  TitlebarFontSize = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarFontSize"), TEXT("13"));
  TitlebarFont = GraphicFunctions::BuildFont(fontName, TitlebarFontSize, TRUE, FALSE);

  TitlebarHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarHeight"), TEXT("16"));

  // titlebar skin items
  TitlebarSkinWIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinWIDTH"), TEXT("200"));
  TitlebarSkinICON_SPACING = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinICON_SPACING"), TEXT("3"));
  TitlebarSkinBATTERY_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_X_OFFSET"), TEXT("0"));
  TitlebarSkinBATTERY_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_WIDTH"), TEXT("21"));
  TitlebarSkinSPEAKER_OFF_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinSPEAKER_OFF_X_OFFSET"), TEXT("23"));
  TitlebarSkinSPEAKER_OFF_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinSPEAKER_OFF_WIDTH"), TEXT("17"));
  TitlebarSkinSPEAKER_ON_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinSPEAKER_ON_X_OFFSET"), TEXT("44"));
  TitlebarSkinSPEAKER_ON_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinSPEAKER_ON_WIDTH"), TEXT("16"));
  TitlebarSkinVIBRATE_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinVIBRATE_X_OFFSET"), TEXT("67"));
  TitlebarSkinVIBRATE_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinVIBRATE_WIDTH"), TEXT("17"));
  TitlebarSkinBLUETOOTH_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBLUETOOTH_X_OFFSET"), TEXT("90"));
  TitlebarSkinBLUETOOTH_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBLUETOOTH_WIDTH"), TEXT("11"));
  TitlebarSkinWIFI_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinWIFI_X_OFFSET"), TEXT("110"));
  TitlebarSkinWIFI_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinWIFI_WIDTH"), TEXT("21"));
  TitlebarSkinLOCK_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinLOCK_X_OFFSET"), TEXT("132"));
  TitlebarSkinLOCK_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinLOCK_WIDTH"), TEXT("8"));
  TitlebarSkinALARM_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinALARM_X_OFFSET"), TEXT("139"));
  TitlebarSkinALARM_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinALARM_WIDTH"), TEXT("10"));
  TitlebarSkinA2DP_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinA2DP_X_OFFSET"), TEXT("152"));
  TitlebarSkinA2DP_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinA2DP_WIDTH"), TEXT("17"));
  TitlebarSkinCONNECTION_X_OFFSET = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinCONNECTION_X_OFFSET"), TEXT("171"));
  TitlebarSkinCONNECTION_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinCONNECTION_WIDTH"), TEXT("17"));
  TitlebarSkinBATTERY_BAR_OFFSETX = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_BAR_OFFSETX"), TEXT("3"));
  TitlebarSkinBATTERY_BAR_OFFSETY = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_BAR_OFFSETY"), TEXT("5"));
  TitlebarSkinBATTERY_BAR_WIDTH = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_BAR_WIDTH"), TEXT("11"));
  TitlebarSkinBATTERY_BAR_HEIGHT = IniReader::getInt(IniReader::sectionSkin, TEXT("TitlebarSkinBATTERY_BAR_HEIGHT"), TEXT("6"));

  hbrListBackground = CreateSolidBrush(IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListBackground"), TEXT("050510")));
  hbrListGroupBackground = CreateSolidBrush(IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListGroupBackground"), TEXT("000000")));
  rgbListGroupText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListGroupText"), TEXT("ffffff"));
  rgbListItemBackground = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemBackground"), TEXT("1e1e1e"));
  hbrListItemBackground = CreateSolidBrush(rgbListItemBackground);
  rgbListItemText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemText"), TEXT("dcdcdc"));
  rgbListItemFavoriteText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemFavoriteText"), TEXT("dcdc00"));
  hbrListItemForeground = CreateSolidBrush(rgbListItemText);
  hpenListItemForeground = CreatePen(PS_SOLID, 1, rgbListItemText);
  rgbListItemSecondaryText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSecondaryText"), TEXT("dcdcdc"));
  rgbListItemSecondaryText2 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSecondaryText2"), TEXT("dcdcdc"));
  rgbListItemNumberText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemNumberText"), TEXT("808080"));
  rgbListItemHoverBackground1 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemHoverBackground1"), TEXT("000000"));
  rgbListItemHoverBackground2 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemHoverBackground2"), TEXT("282828"));
  rgbListItemSelectedBackground1 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSelectedBackground1"), TEXT("505050"));
  rgbListItemSelectedBackground2 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSelectedBackground2"), TEXT("101010"));
  rgbListItemSelectedText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSelectedText"), TEXT("e6e6e6"));
  rgbListItemSelectedShadow = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSelectedShadow"), TEXT("505050"));
  hbrListItemSeparator = CreateSolidBrush(IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListItemSeparator"), TEXT("323232")));
  rgbListIndicatorText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListIndicatorText"), TEXT("ffffff"));
  rgbListScrollbarIndicator = IniReader::getColor(
    IniReader::sectionSkin, TEXT("ListScrollbarIndicator"), TEXT("505050"));
  hbrListScrollbarIndicator = CreateSolidBrush(rgbListScrollbarIndicator);
  rgbLongTap = IniReader::getColor(
    IniReader::sectionSkin, TEXT("LongTap"), TEXT("a0a0a0"));
  hbrLongTap = CreateSolidBrush(rgbLongTap);
  rgbLongTapFull = IniReader::getColor(
    IniReader::sectionSkin, TEXT("LongTapFull"), TEXT("a0ffa0"));
  hbrLongTapFull = CreateSolidBrush(rgbLongTapFull);

  rgbCallHistoryMissed = IniReader::getColor(
    IniReader::sectionSkin, TEXT("CallHistoryMissed"), TEXT("ff8080"));
  rgbCallHistoryIncoming = IniReader::getColor(
    IniReader::sectionSkin, TEXT("CallHistoryIncoming"), TEXT("ffffdc"));
  rgbCallHistoryOutgoing = IniReader::getColor(
    IniReader::sectionSkin, TEXT("CallHistoryOutgoing"), TEXT("dcffdc"));

  rgbDetailsBackground1 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsBackground1"), TEXT("404040"));
  rgbDetailsBackground2 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsBackground2"), TEXT("000000"));
  hbrDetailsBackground = CreateSolidBrush(GraphicFunctions::GetMedianColor(rgbDetailsBackground1, rgbDetailsBackground2));
  rgbDetailsHoverBackground1 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsHoverBackground1"), TEXT("707070"));
  rgbDetailsHoverBackground2 = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsHoverBackground2"), TEXT("202020"));
  rgbDetailsHeaderShadow = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsHeaderShadow"), TEXT("505050"));
  rgbDetailsHeaderText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsHeaderText"), TEXT("e6e6e6"));
  rgbDetailsItemShadow = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsItemShadow"), TEXT("505050"));
  rgbDetailsItemText = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsItemText"), TEXT("e6e6e6"));
  rgbDetailsDefaultNumber = IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailsDefaultNumber"), TEXT("f6f6e6"));
  hbrDetailItemSeparator = CreateSolidBrush(IniReader::getColor(
    IniReader::sectionSkin, TEXT("DetailItemSeparator"), TEXT("999999")));

  rgbPopupText = IniReader::getColor(IniReader::sectionSkin, TEXT("PopupText"), TEXT("dcdcdc"));
  rgbPopupBackground = IniReader::getColor(IniReader::sectionSkin, TEXT("PopupBackground"), TEXT("464646"));
  rgbPopupGrid = IniReader::getColor(IniReader::sectionSkin, TEXT("PopupGrid"), TEXT("646464"));
  hbrPopupForeground = CreateSolidBrush(rgbPopupText);
  hbrPopupBackground = CreateSolidBrush(rgbPopupBackground);
  hpenPopupGrid = CreatePen(PS_SOLID, 1, rgbPopupGrid);

  // read menu data
  MenuStretch = IniReader::getBool(IniReader::sectionSkin, TEXT("MenuStretch"), TEXT("0"));
  iMenuButtonWidth = IniReader::getInt(IniReader::sectionSkin, TEXT("MenuButtonWidth"), TEXT("48"));
  iMenuButtonHeight = IniReader::getInt(IniReader::sectionSkin, TEXT("MenuButtonHeight"), TEXT("40"));
  iMenuSkinY = IniReader::getInt(IniReader::sectionSkin, TEXT("MenuOffset"), TEXT("16"));
  if (iMenuButtonWidth == 0)
    iMenuButtonsPerRow = 1;
  else
    iMenuButtonsPerRow = Theme::iSkinWidth / Theme::iMenuButtonWidth;

  // detail icons
  iDetailIconsY = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailIconsY"), TEXT("288"));
  iDetailIconsW = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailIconsW"), TEXT("32"));
  iDetailIconsH = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailIconsH"), TEXT("32"));
  if (iDetailIconsW == 0)
    iDetailIconsPerPlane = 0;
  else
    iDetailIconsPerPlane = iSkinWidth / iDetailIconsW;

  iDetailSmallIconsY = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailSmallIconsY"), TEXT("0"));
  iDetailSmallIconsW = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailSmallIconsW"), TEXT("0"));
  iDetailSmallIconsH = IniReader::getInt(IniReader::sectionSkin, TEXT("DetailSmallIconsH"), TEXT("0"));
  HasSmallIcons = iDetailSmallIconsY > 0;
}

void Theme::Destroy()
{
  if (hdcSkin != NULL)
  {
    DeleteDC(hdcSkin);
    hdcSkin = NULL;
  }
  Delete(hbmSkin);
  Delete(hbrTitlebarBackground);
  Delete(hbrTitlebarText);
  Delete(hbrTitlebarSignal);
  Delete(hbrTitlebarBattery);
  Delete(TitlebarFont);
  Delete(hbrListBackground);
  Delete(hbrListGroupBackground);
  Delete(hbrListItemBackground);
  Delete(hbrListItemForeground);
  Delete(hbrListItemSeparator);
  Delete(hbrListScrollbarIndicator);
  Delete(hbrDetailsBackground);
  Delete(hbrDetailItemSeparator);
  Delete(hbrPopupForeground);
  Delete(hbrPopupBackground);
  Delete(hpenPopupGrid);
  Delete(PrimaryListFont);
  Delete(DialingNoListFont);
  Delete(SecondaryListFont);
  Delete(DetailsHeaderFont);
  Delete(DetailsHeaderFont2);
  Delete(DetailsItemFont);
  Delete(DetailsFooterFont);
  Delete(GroupFont);
  Delete(ListIndicatorFont);
  Delete(KeyboardFont);
  Delete(T9ButtonsFont);
  Delete(T9TextFont);
  Delete(CategoriesFont);
}

void Theme::DrawMenuItem(HDC hdc, int x, int y, int cx, int cy, EMenuItems item, bool selected)
{
  if (item >= miCount || Theme::iMenuButtonWidth == 0)
    return;

  int sx = (item % Theme::iMenuButtonsPerRow) * Theme::iMenuButtonWidth;
  int sy = Theme::iMenuSkinY + (item / Theme::iMenuButtonsPerRow) * Theme::iMenuButtonHeight;
  if (selected)
    sy += (miCount / Theme::iMenuButtonsPerRow) * Theme::iMenuButtonHeight;

  if (cx != 0 || cy != 0)
    StretchBlt(hdc, x, y, cx, cy,
       Theme::hdcSkin, sx, sy, Theme::iMenuButtonWidth, Theme::iMenuButtonHeight,
       SRCCOPY);
  else
    BitBlt(hdc, x, y, Theme::iMenuButtonWidth, Theme::iMenuButtonHeight,
       Theme::hdcSkin, sx, sy,
       SRCCOPY);
}

void Theme::DrawMenuBg(HDC hdc, int x, int y, int cx, int cy, EMenuItems item)
{
  if (item >= miCount || Theme::iMenuButtonWidth == 0)
    return;

  int sy = Theme::iMenuSkinY + (item / Theme::iMenuButtonsPerRow) * Theme::iMenuButtonHeight;

  StretchBlt(hdc, x, y, cx, cy,
     Theme::hdcSkin, 0, sy, 1, Theme::iMenuButtonHeight,
     SRCCOPY);
}

void Theme::DrawSpecialIcon(HDC hdc, int x, int y, ESpecialIcons icon)
{
  if (icon < 0 || icon >= siCount)
    return;

  TransparentBlt(hdc,
         x, y, Theme::SpecialIconsWidth, Theme::SpecialIconsHeight,
         Theme::hdcSkin,
         icon * Theme::SpecialIconsWidth, SpecialIconsY,
         Theme::SpecialIconsWidth, Theme::SpecialIconsHeight,
         rgbTransparentColor);
}

void Theme::DrawDetailsIcon(HDC hdc, int x, int y, EDetailsIcon icon)
{
  if (icon < 0 || icon >= diCount)
    return;
  int tx, ty, w, h;

  if (IsSmallIcon(icon)) 
  {
    w = Theme::iDetailSmallIconsW;
    h = Theme::iDetailSmallIconsH;
    tx = GetSmalIconIndex(icon) * Theme::iDetailSmallIconsW;
    ty = Theme::iDetailSmallIconsY;
  } 
  else 
  {
    w = Theme::iDetailIconsW;
    h = Theme::iDetailIconsH;
    tx = (icon % Theme::iDetailIconsPerPlane) * w;
    ty = Theme::iDetailIconsY + (icon / Theme::iDetailIconsPerPlane) * h;
  }

  TransparentBlt(hdc,
         x, y, w, h,
         Theme::hdcSkin,
         tx, ty,
         w, h,
         rgbTransparentColor);
}

int Theme::GetSmalIconIndex(EDetailsIcon icon)
{
  switch (icon)
  {
    case diBirthday: return 0;
    case diAnniversary: return 1;
    case diRingtone: return 2;
  }
  return -1;
}

bool Theme::IsSmallIcon(EDetailsIcon icon)
{
  return (GetSmalIconIndex(icon) >= 0);
}

void Theme::Delete(HGDIOBJ obj)
{
  if (obj != NULL)
    DeleteObject(obj);
  obj = NULL;
}
